﻿#region Copyright Notice
/*
Copyright (C) 2011 by Ashley Ross

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion Copyright Notice

using System;
using System.Runtime.InteropServices;

namespace Manta.IO
{
    /// <summary>
    /// This class is useful for examining reparse point data.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public class ReparseRawDataBuffer : IReparseDataBuffer
    {
        // * Reparse point tag
        private ReparseTag _reparseTag;
        // * Size of the reparse data in the DataBuffer member, in bytes.
        private ushort _reparseDataLength;
        // * Reserved; do not use. :)
        private ushort _reserved;
        // Raw data for the reparse point. The contents are determined by the reparse point implementer.
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaximumBufferSize)]
        private byte[] _rawBuffer;
        

        // Size, in bytes, of the fields marked *
        private const int HeaderSize = 8;
        // Maximum size, in bytes, of the _rawBuffer field
        private const int MaximumBufferSize = 0x3FF8;


        /// <summary>
        /// Initializes a new instance of the ReparseRawDataBuffer class.
        /// </summary>
        public ReparseRawDataBuffer()
        {
            _rawBuffer = new byte[MaximumBufferSize];
        }

        /// <summary>
        /// Initializes a new instance of the ReparseRawDataBuffer class with the specified reparse tag.
        /// </summary>
        /// <param name="tag">The reparse tag to initialize the ReparseDataBuffer with.</param>
        public ReparseRawDataBuffer(ReparseTag tag)
            : this()
        {
            _reparseTag = tag;
        }

        /// <summary>
        /// Initializes a new instance of the ReparseRawDataBuffer class with the specified reparse tag.
        /// </summary>
        /// <param name="tag">The reparse tag to initialize the ReparseDataBuffer with.</param>
        public ReparseRawDataBuffer(uint tag)
            : this((ReparseTag)tag)
        {
        }


        /// <summary>
        /// Gets or sets the reparse tag of the current ReparseRawDataBuffer.
        /// </summary>
        public ReparseTag Tag
        {
            get { return _reparseTag; }
            set { _reparseTag = value; }
        }

        /// <summary>
        /// Gets or sets the reserved field of the current ReparseRawDataBuffer.
        /// </summary>
        public ushort Reserved
        {
            get { return _reserved; }
            set { _reserved = value; }
        }

        /// <summary>
        /// Gets or sets the raw buffer of the current ReparseRawDataBuffer.
        /// </summary>
        public byte[] RawBuffer
        {
            get { return _rawBuffer; }
            set            {

                if (value != null && value.Length > MaximumBufferSize)
                    throw new ArgumentException("The length of the data buffer cannot be larger than MaximumBufferSize.");

                _rawBuffer = value;
                _reparseDataLength = value == null ? (ushort)0 : (ushort)value.Length;
            }
        }

        /// <summary>
        /// Gets the size, in bytes, of the current ReparseDataBuffer.
        /// </summary>
        public int Size
        {
            get { return HeaderSize + _reparseDataLength; }
        }
    }
}
